package com.whm.hrmanagement.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whm.common.constant.UserConstants;
import com.whm.common.core.domain.entity.SysUser;
import com.whm.common.core.domain.model.LoginUser;
import com.whm.common.utils.DateUtils;
import com.whm.common.utils.StringUtils;
import com.whm.hrmanagement.domain.dto.CommonDto;
import com.whm.hrmanagement.domain.dto.Extra;
import com.whm.hrmanagement.domain.dto.UserSelectListDto;
import com.whm.system.domain.SysFileInfo;
import com.whm.system.mapper.SysDeptMapper;
import com.whm.system.mapper.SysFileInfoMapper;
import com.whm.system.service.ISysFileInfoService;
import io.lettuce.core.ScriptOutputType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import com.whm.hrmanagement.mapper.OaZrsglrydaMapper;
import com.whm.hrmanagement.domain.OaZrsglryda;
import com.whm.hrmanagement.service.IOaZrsglrydaService;

import static com.whm.common.utils.PageUtils.startPage;

/**
 * hrmanagementService业务层处理
 * 
 * @author whm
 * @date 2024-01-30
 */
@Service
public class OaZrsglrydaServiceImpl implements IOaZrsglrydaService 
{
    @Autowired
    private OaZrsglrydaMapper oaZrsglrydaMapper;
    @Autowired
    private SysFileInfoMapper sysFileInfoMapper;
    @Autowired
    private ISysFileInfoService iSysFileInfoService;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    /**
     * 查询hrmanagement
     * 
     * @param id hrmanagement主键
     * @return hrmanagement
     */
    @Override
    public OaZrsglryda selectOaZrsglrydaById(Long id)
    {
        return oaZrsglrydaMapper.selectOaZrsglrydaById(id);
    }

    /**
     * 查询hrmanagement列表
     * 
     * @param oaZrsglryda hrmanagement
     * @return hrmanagement
     */
    @Override
    public List<OaZrsglryda> selectOaZrsglrydaList(OaZrsglryda oaZrsglryda)
    {

        LoginUser user = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user.getDeptId() != 204 && user.getDeptId() != 200 &&  user.getUserId() != 109 && user.getUserId() != 1) {
            List<OaZrsglryda> oaZrsglrydaList = oaZrsglrydaMapper.selectOaZrsglrydaList(oaZrsglryda);

            return oaZrsglrydaList.stream().filter(e -> e.getYuanGongHao() == user.getUserId()).collect(Collectors.toList());
        }
        startPage();
        return oaZrsglrydaMapper.selectOaZrsglrydaList(oaZrsglryda);
    }

    @Override
    public List<OaZrsglryda> selectOaZrsglrydaList2(OaZrsglryda oaZrsglryda) {
            LoginUser user = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            if (user.getDeptId() != 204 && user.getDeptId() != 200 &&  user.getUserId() != 109 && user.getUserId() != 1) {
                List<OaZrsglryda> oaZrsglrydaList = oaZrsglrydaMapper.selectOaZrsglrydaList(oaZrsglryda);

                return oaZrsglrydaList.stream().filter(e -> e.getYuanGongHao() == user.getUserId()).collect(Collectors.toList());
            }

            return oaZrsglrydaMapper.selectOaZrsglrydaList(oaZrsglryda);
    }

    /**
     * 新增hrmanagement
     * 
     * @param oaZrsglryda hrmanagement
     * @return 结果
     */
    @Override
    public int insertOaZrsglryda(JSONObject data)
    {


        String jsonString = JSONObject.toJSONString(data.getJSONObject("form"));
        OaZrsglryda oaZrsglryda = JSONObject.parseObject(jsonString, OaZrsglryda.class);


        String DepartName = sysDeptMapper.getDeptNameById(oaZrsglryda.getDepartID());
        oaZrsglryda.setDepartName(DepartName);

        String jsonString2 = JSONArray.toJSONString(data.getJSONArray("Extra"));
        List<Extra> extras = JSONArray.parseArray(jsonString2,  Extra.class);

        Integer result = oaZrsglrydaMapper.insertOaZrsglryda(oaZrsglryda);
        QueryWrapper<SysFileInfo> qw = new QueryWrapper<>();

        for (Extra obj : extras) {
            System.out.println(obj.getFilePath());
            qw.lambda().eq(SysFileInfo::getFilePath,obj.getFilePath());

            SysFileInfo  sysFileInfo =  sysFileInfoMapper.selectOne(qw);
            sysFileInfo.setReltype("OaZrsglryda");
            sysFileInfo.setRelId(oaZrsglryda.getId());

            iSysFileInfoService.updateSysFileInfo(sysFileInfo);

        }
        return result;
    }

    /**
     * 修改hrmanagement
     * 
     * @param oaZrsglryda hrmanagement
     * @return 结果
     */
    @Override
    public int updateOaZrsglryda(JSONObject data)
    {



        String jsonString = JSONObject.toJSONString(data.getJSONObject("form"));
        OaZrsglryda oaZrsglryda = JSONObject.parseObject(jsonString, OaZrsglryda.class);
        String DepartName = sysDeptMapper.getDeptNameById(oaZrsglryda.getDepartID());
        oaZrsglryda.setDepartName(DepartName);
        String jsonString2 = JSONArray.toJSONString(data.getJSONArray("Extra"));
        List<Extra> extras = JSONArray.parseArray(jsonString2,  Extra.class);
        QueryWrapper<SysFileInfo> qw = new QueryWrapper<>();
        qw.lambda().eq(SysFileInfo::getRelId,oaZrsglryda.getId());
        List<SysFileInfo>  sysFileInfoList =  sysFileInfoMapper.selectList(qw);
        for (SysFileInfo obj : sysFileInfoList) {
            obj.setRelId(0L);
            iSysFileInfoService.updateSysFileInfo(obj);
        }
        System.out.println("数据成功插入");

        for (Extra obj2 : extras) {
            QueryWrapper<SysFileInfo> qw2 = new QueryWrapper<>();
            qw2.lambda().eq(SysFileInfo::getFilePath,obj2.getFilePath());

            SysFileInfo  sysFileInfo2 =  sysFileInfoMapper.selectOne(qw2);;
            sysFileInfo2.setReltype("OaZrsglryda");
            sysFileInfo2.setRelId(oaZrsglryda.getId());

            iSysFileInfoService.updateSysFileInfo(sysFileInfo2);

        }

        oaZrsglryda.setUpdateTime(DateUtils.getNowDate());
        return oaZrsglrydaMapper.updateOaZrsglryda(oaZrsglryda);
    }

    /**
     * 批量删除hrmanagement
     * 
     * @param ids 需要删除的hrmanagement主键
     * @return 结果
     */
    @Override
    public int deleteOaZrsglrydaByIds(Long[] ids)
    {
        return oaZrsglrydaMapper.deleteOaZrsglrydaByIds(ids);
    }

    /**
     * 删除hrmanagement信息
     * 
     * @param id hrmanagement主键
     * @return 结果
     */
    @Override
    public int deleteOaZrsglrydaById(Long id)
    {
        return oaZrsglrydaMapper.deleteOaZrsglrydaById(id);
    }

    @Override
    public List<UserSelectListDto> getUserSelect() {

        List<OaZrsglryda> oaZrsglrydaList =  oaZrsglrydaMapper.selectOaZrsglrydaList(null);

        List<UserSelectListDto> userSelectListDtos =  new ArrayList<>();
        oaZrsglrydaList.forEach(item->userSelectListDtos.add((new UserSelectListDto(item.getYuanGongHao(),item.getXingMing()))));





        return userSelectListDtos;
    }

    @Override
    public boolean checkNickNameUnique(OaZrsglryda oaZrsglryda) {
        Long yuanhonghaoId = StringUtils.isNull(oaZrsglryda.getYuanGongHao()) ? -1L : oaZrsglryda.getYuanGongHao();
        OaZrsglryda oaZrsglryda1 = oaZrsglrydaMapper.checkyuanhonghaoUnique(yuanhonghaoId);
        if (StringUtils.isNotNull(oaZrsglryda1) )
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
